package com.cqcet.wenchuang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqcet.wenchuang.common.customException.CustomException;
import com.cqcet.wenchuang.dto.OrderDetailsDto;
import com.cqcet.wenchuang.dto.OrderDto;
import com.cqcet.wenchuang.entity.*;
import com.cqcet.wenchuang.mapper.OrdersMapper;
import com.cqcet.wenchuang.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Transactional
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrderDetailsService orderDetailsService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;

    /**
     * 保存订单同时保存订单详细
     * 必须参数：商品id，用户id，地址id，数量
     *
     * @param orderDto
     */
    @Override
    public void saveWithDetails(OrderDto orderDto) {
        //下单时间
        orderDto.setOrderTime(LocalDateTime.now());
        //待支付
        orderDto.setStatus(1);
        this.save(orderDto);

        AddressBook addressBook = addressBookService.getById(orderDto.getAddressId());

        List<OrderDetails> collect = orderDto.getOrderDetailsDtoList().stream().peek(orderDetails -> {
            Goods goods = goodsService.getById(orderDetails.getGoodsId());
            if (goods.getQuantity() < orderDetails.getQuantity()) {
                throw new CustomException("商品：" + goods.getName() + " 数量不足");
            }
            //商品数量减少
            goods.setQuantity(goods.getQuantity() - orderDetails.getQuantity());
            goodsService.updateById(goods);

            orderDetails.setOrderId(orderDto.getId());
            orderDetails.setCode(UUID.randomUUID().toString());
            orderDetails.setAdminId(goods.getAdminId());
            orderDetails.setTotalPrices(goods.getPrice() * orderDetails.getQuantity());
            orderDetails.setProvince(addressBook.getProvince());
            orderDetails.setCity(addressBook.getCity());
            orderDetails.setDetails(addressBook.getDetails());
            orderDetails.setPhone(addressBook.getPhone());
            orderDetails.setConsignee(addressBook.getConsignee());
        }).collect(Collectors.toList());

        orderDetailsService.saveBatch(collect);
    }

    /**
     * 从购物车下单，可同时生成多个订单和订单详情
     *
     * @param orderDto
     */
    @Override
    public void saveFromShoppingCart(OrderDto orderDto) {
        //购物车ids
        List<Long> shoppingCartIds = orderDto.getShoppingCartIds();
        ShoppingCart cart = shoppingCartService.getById(shoppingCartIds.get(0));
        //设置用户id
        User user = userService.getById(cart.getUserId());
        orderDto.setUserId(user.getId());
        //遍历购物车
        //orderDto能直接获取到的：地址id，用户id，数量;能间接获取到的：商品id
        List<OrderDetailsDto> orderDetailsDtos = shoppingCartIds.stream().map((shoppingCartId) -> {
            OrderDetailsDto orderDetailsDto = new OrderDetailsDto();
            ShoppingCart shoppingCart = shoppingCartService.getById(shoppingCartId);
            orderDetailsDto.setQuantity(shoppingCart.getQuantity());
            orderDetailsDto.setGoodsId(shoppingCart.getGoodsId());
            return orderDetailsDto;
        }).collect(Collectors.toList());

        orderDto.setOrderDetailsDtoList(orderDetailsDtos);

        this.saveWithDetails(orderDto);
    }
}
